package com.imooc.security.ecdsa;

import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class ImoocECDSA {

    public static String src = "imooc security ecdsa";

    public static void main(String[] args) {
        jdkECDSA();
        bcECDSA();
    }

    public static void jdkECDSA() {
        try {
            //1.初始化密钥
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
            keyPairGenerator.initialize(256);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            ECPublicKey ecdsaPublicKey = (ECPublicKey) keyPair.getPublic();
            ECPrivateKey ecdsaPrivateKey = (ECPrivateKey) keyPair.getPrivate();

            //2.执行签名
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec((ecdsaPrivateKey.getEncoded()));
            KeyFactory keyFactory = KeyFactory.getInstance("EC");// ctrl + alt + v 自动填充左边
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
            Signature signature = Signature.getInstance("SHA1withECDSA");
            signature.initSign(privateKey);
            signature.update(src.getBytes());
            byte[] result = signature.sign();
            System.out.println("jdk ecdsa sign:" + Hex.encodeHexString(result));

            //3.验证签名
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecdsaPublicKey.getEncoded());
            keyFactory = KeyFactory.getInstance("EC");
            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
            signature = Signature.getInstance("SHA1withECDSA");
            signature.initVerify(publicKey);
            signature.update(src.getBytes());
            boolean bool = signature.verify(result);
            System.out.println("jdk ecdsa verify:" + bool);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void bcECDSA() {
        try {
            Security.addProvider(new BouncyCastleProvider());

            //1.初始化密钥
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
            keyPairGenerator.initialize(224);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            ECPublicKey ecdsaPublicKey = (ECPublicKey) keyPair.getPublic();
            ECPrivateKey ecdsaPrivateKey = (ECPrivateKey) keyPair.getPrivate();

            //2.执行签名
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec((ecdsaPrivateKey.getEncoded()));
            KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");// ctrl + alt + v 自动填充左边
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
            Signature signature = Signature.getInstance("SHA224withECDSA");
            signature.initSign(privateKey);
            signature.update(src.getBytes());
            byte[] result = signature.sign();
            System.out.println("bc ecdsa sign:" + Hex.encodeHexString(result));

            //3.验证签名
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(ecdsaPublicKey.getEncoded());
            keyFactory = KeyFactory.getInstance("EC", "BC");
            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
            signature = Signature.getInstance("SHA224withECDSA");
            signature.initVerify(publicKey);
            signature.update(src.getBytes());
            boolean bool = signature.verify(result);
            System.out.println("bc ecdsa verify:" + bool);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
